package com.pronosticnews.client.admin.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.event.shared.EventBus;
import com.pronosticnews.client.admin.ClientFactory;
import com.pronosticnews.client.admin.view.editClub.EditClubView;
import com.pronosticnews.client.admin.view.editClub.EditClubView.Presenter;
import com.pronosticnews.client.event.DeletedClubEvent;
import com.pronosticnews.client.event.DeletedClubEventHandler;
import com.pronosticnews.client.event.InformationMessageEvent;
import com.pronosticnews.client.event.NouveauClubEvent;
import com.pronosticnews.client.event.SelectedClubEvent;
import com.pronosticnews.client.event.SelectedClubEventHandler;
import com.pronosticnews.client.services.ClubService;
import com.pronosticnews.client.services.ClubServiceAsync;
import com.pronosticnews.client.services.PaysService;
import com.pronosticnews.client.services.PaysServiceAsync;
import com.pronosticnews.client.util.InformationMessage;
import com.pronosticnews.client.util.InformationMessage.TYPE_MESSAGE;
import com.pronosticnews.shared.model.ClubDto;
import com.pronosticnews.shared.model.PaysDto;

import fr.ysa.services.config.ConfigRequetesDto;
import fr.ysa.services.config.TrieDto;
import fr.ysa.services.config.TrieDto.ORDRE;

public class EditClubActivity extends AbstractActivity implements Presenter {

	private EditClubView view;

	private PlaceController placeController;

	private ClubServiceAsync clubService;

	private AsyncCallback<ClubDto> saveCallBack;

	private EventBus eventBus;

	private ClubDto existingClub;

	private AsyncCallback<List<PaysDto>> getPaysCallBack;

	private ConfigRequetesDto configGetPays;
	
	private PaysServiceAsync paysService;

	public EditClubActivity(ClientFactory clientFactory) {
		clubService = GWT.create(ClubService.class);
		paysService = GWT.create(PaysService.class);
		placeController = clientFactory.getPlaceController();
		view = clientFactory.getEditClubView();
		view.setPresenter(this);
		eventBus = clientFactory.getEventBus();

		saveCallBack = new AsyncCallback<ClubDto>() {

			@Override
			public void onSuccess(ClubDto result) {
				InformationMessage messageSuccess = new InformationMessage();
				messageSuccess.setType(TYPE_MESSAGE.SUCCESS);
				List<String> messages = new ArrayList<String>();
				messages.add("Le club a été créé/modifié avec succès !");
				messageSuccess.setText(messages);
				view.submitComplete();
				eventBus.fireEvent(new NouveauClubEvent());
				eventBus.fireEvent(new InformationMessageEvent(messageSuccess));
			}

			@Override
			public void onFailure(Throwable caught) {
				InformationMessage erreur = new InformationMessage();
				List<String> messages = new ArrayList<String>();
				messages.add(caught.getMessage());
				erreur.setText(messages);
				erreur.setType(TYPE_MESSAGE.ERROR);
				eventBus.fireEvent(new InformationMessageEvent(erreur));
			}
		};

		eventBus.addHandler(SelectedClubEvent.TYPE,
				new SelectedClubEventHandler() {

					@Override
					public void onSelect(SelectedClubEvent event) {
						existingClub = event.getClubSelected();
						view.setModeModification(true);
						view.setClub(existingClub);
					}
				});

		eventBus.addHandler(DeletedClubEvent.TYPE,
				new DeletedClubEventHandler() {

					@Override
					public void onDelete(DeletedClubEvent event) {
						existingClub = null;
						view.setModeModification(false);
					}
				});

		view.setModeModification(false);

	}

	@Override
	public void start(AcceptsOneWidget panel,
			com.google.gwt.event.shared.EventBus eventBus) {
		getPays();
		panel.setWidget(view.asWidget());
	}

	@Override
	public void submit(ClubDto editClub) {
		if (existingClub != null) {
			editClub.setId(existingClub.getId());
			clubService.modifier(editClub, saveCallBack);
		} else {
			clubService.sauvegarder(editClub, saveCallBack);
		}
	}

	@Override
	public void annuleModification() {
		existingClub = null;
	}

	@Override
	public void getPays() {
		if (getPaysCallBack == null) {
			getPaysCallBack = new AsyncCallback<List<PaysDto>>() {

				@Override
				public void onSuccess(List<PaysDto> result) {
					view.setPays(result);
				}

				@Override
				public void onFailure(Throwable caught) {
					InformationMessage erreur = new InformationMessage();
					erreur.setType(TYPE_MESSAGE.WARNING);
					List<String> messages = new ArrayList<String>();
					messages.add(caught.getMessage());
					erreur.setText(messages);
					eventBus.fireEvent(new InformationMessageEvent(erreur));
				}
			};
		}
		if (configGetPays == null) {
			configGetPays = new ConfigRequetesDto();
			TrieDto trieAsc = new TrieDto();
			trieAsc.setOrdre(ORDRE.ASC);
			trieAsc.setChamp(PaysDto.VALUE_NAME.nom.name());
			configGetPays.getTries().add(trieAsc);
		}
		paysService.trouver(configGetPays, getPaysCallBack);
	}

}
